
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.db.migrations.operations.special &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../../" src="../../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../../search.html" />



 
<script type="text/javascript" src="../../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-db-migrations-operations-special">
            
  <h1>Source code for django.db.migrations.operations.special</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">router</span>

<span class="kn">from</span> <span class="nn">.base</span> <span class="k">import</span> <span class="n">Operation</span>


<div class="viewcode-block" id="SeparateDatabaseAndState"><a class="viewcode-back" href="../../../../../ref/migration-operations.html#django.db.migrations.operations.SeparateDatabaseAndState">[docs]</a><span class="k">class</span> <span class="nc">SeparateDatabaseAndState</span><span class="p">(</span><span class="n">Operation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Take two lists of operations - ones that will be used for the database,</span>
<span class="sd">    and ones that will be used for the state change. This allows operations</span>
<span class="sd">    that don&#39;t support state change to have it applied, or have operations</span>
<span class="sd">    that affect the state or not the database, or so on.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">serialization_expand_args</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;database_operations&#39;</span><span class="p">,</span> <span class="s1">&#39;state_operations&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">database_operations</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">state_operations</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">database_operations</span> <span class="o">=</span> <span class="n">database_operations</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">state_operations</span> <span class="o">=</span> <span class="n">state_operations</span> <span class="ow">or</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">database_operations</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;database_operations&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">database_operations</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_operations</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;state_operations&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_operations</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__qualname__</span><span class="p">,</span>
            <span class="p">[],</span>
            <span class="n">kwargs</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">state_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">state_operation</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_operations</span><span class="p">:</span>
            <span class="n">state_operation</span><span class="o">.</span><span class="n">state_forwards</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">state</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="c1"># We calculate state separately in here since our state functions aren&#39;t useful</span>
        <span class="k">for</span> <span class="n">database_operation</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">database_operations</span><span class="p">:</span>
            <span class="n">to_state</span> <span class="o">=</span> <span class="n">from_state</span><span class="o">.</span><span class="n">clone</span><span class="p">()</span>
            <span class="n">database_operation</span><span class="o">.</span><span class="n">state_forwards</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">to_state</span><span class="p">)</span>
            <span class="n">database_operation</span><span class="o">.</span><span class="n">database_forwards</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">)</span>
            <span class="n">from_state</span> <span class="o">=</span> <span class="n">to_state</span>

    <span class="k">def</span> <span class="nf">database_backwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="c1"># We calculate state separately in here since our state functions aren&#39;t useful</span>
        <span class="n">to_states</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">dbop</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">database_operations</span><span class="p">:</span>
            <span class="n">to_states</span><span class="p">[</span><span class="n">dbop</span><span class="p">]</span> <span class="o">=</span> <span class="n">to_state</span>
            <span class="n">to_state</span> <span class="o">=</span> <span class="n">to_state</span><span class="o">.</span><span class="n">clone</span><span class="p">()</span>
            <span class="n">dbop</span><span class="o">.</span><span class="n">state_forwards</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">to_state</span><span class="p">)</span>
        <span class="c1"># to_state now has the states of all the database_operations applied</span>
        <span class="c1"># which is the from_state for the backwards migration of the last</span>
        <span class="c1"># operation.</span>
        <span class="k">for</span> <span class="n">database_operation</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">database_operations</span><span class="p">):</span>
            <span class="n">from_state</span> <span class="o">=</span> <span class="n">to_state</span>
            <span class="n">to_state</span> <span class="o">=</span> <span class="n">to_states</span><span class="p">[</span><span class="n">database_operation</span><span class="p">]</span>
            <span class="n">database_operation</span><span class="o">.</span><span class="n">database_backwards</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">describe</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;Custom state/database change combination&quot;</span></div>


<div class="viewcode-block" id="RunSQL"><a class="viewcode-back" href="../../../../../ref/migration-operations.html#django.db.migrations.operations.RunSQL">[docs]</a><span class="k">class</span> <span class="nc">RunSQL</span><span class="p">(</span><span class="n">Operation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Run some raw SQL. A reverse SQL statement may be provided.</span>

<span class="sd">    Also accept a list of operations that represent the state change effected</span>
<span class="sd">    by this SQL change, in case it&#39;s custom column/table creation/deletion.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">noop</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sql</span><span class="p">,</span> <span class="n">reverse_sql</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">state_operations</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">hints</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">elidable</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sql</span> <span class="o">=</span> <span class="n">sql</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reverse_sql</span> <span class="o">=</span> <span class="n">reverse_sql</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">state_operations</span> <span class="o">=</span> <span class="n">state_operations</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">hints</span> <span class="o">=</span> <span class="n">hints</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">elidable</span> <span class="o">=</span> <span class="n">elidable</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;sql&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">reverse_sql</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;reverse_sql&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reverse_sql</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_operations</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;state_operations&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_operations</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">hints</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;hints&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">hints</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__qualname__</span><span class="p">,</span>
            <span class="p">[],</span>
            <span class="n">kwargs</span>
        <span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">reversible</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">reverse_sql</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">state_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">state_operation</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_operations</span><span class="p">:</span>
            <span class="n">state_operation</span><span class="o">.</span><span class="n">state_forwards</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">state</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">router</span><span class="o">.</span><span class="n">allow_migrate</span><span class="p">(</span><span class="n">schema_editor</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">hints</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_run_sql</span><span class="p">(</span><span class="n">schema_editor</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_backwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">reverse_sql</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;You cannot reverse this operation&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">router</span><span class="o">.</span><span class="n">allow_migrate</span><span class="p">(</span><span class="n">schema_editor</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">hints</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_run_sql</span><span class="p">(</span><span class="n">schema_editor</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">reverse_sql</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">describe</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;Raw SQL operation&quot;</span>

    <span class="k">def</span> <span class="nf">_run_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">sqls</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">sqls</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
            <span class="k">for</span> <span class="n">sql</span> <span class="ow">in</span> <span class="n">sqls</span><span class="p">:</span>
                <span class="n">params</span> <span class="o">=</span> <span class="kc">None</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
                    <span class="n">elements</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">elements</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                        <span class="n">sql</span><span class="p">,</span> <span class="n">params</span> <span class="o">=</span> <span class="n">sql</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Expected a 2-tuple but got </span><span class="si">%d</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">elements</span><span class="p">)</span>
                <span class="n">schema_editor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">sqls</span> <span class="o">!=</span> <span class="n">RunSQL</span><span class="o">.</span><span class="n">noop</span><span class="p">:</span>
            <span class="n">statements</span> <span class="o">=</span> <span class="n">schema_editor</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">prepare_sql_script</span><span class="p">(</span><span class="n">sqls</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">statement</span> <span class="ow">in</span> <span class="n">statements</span><span class="p">:</span>
                <span class="n">schema_editor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">statement</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span></div>


<div class="viewcode-block" id="RunPython"><a class="viewcode-back" href="../../../../../ref/migration-operations.html#django.db.migrations.operations.RunPython">[docs]</a><span class="k">class</span> <span class="nc">RunPython</span><span class="p">(</span><span class="n">Operation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Run Python code in a context suitable for doing versioned ORM operations.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">reduces_to_sql</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">code</span><span class="p">,</span> <span class="n">reverse_code</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">atomic</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">hints</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">elidable</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">atomic</span> <span class="o">=</span> <span class="n">atomic</span>
        <span class="c1"># Forwards code</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">callable</span><span class="p">(</span><span class="n">code</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;RunPython must be supplied with a callable&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">code</span> <span class="o">=</span> <span class="n">code</span>
        <span class="c1"># Reverse code</span>
        <span class="k">if</span> <span class="n">reverse_code</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reverse_code</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">callable</span><span class="p">(</span><span class="n">reverse_code</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;RunPython must be supplied with callable arguments&quot;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reverse_code</span> <span class="o">=</span> <span class="n">reverse_code</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">hints</span> <span class="o">=</span> <span class="n">hints</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">elidable</span> <span class="o">=</span> <span class="n">elidable</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;code&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">reverse_code</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;reverse_code&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reverse_code</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">atomic</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;atomic&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">atomic</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">hints</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;hints&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">hints</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__qualname__</span><span class="p">,</span>
            <span class="p">[],</span>
            <span class="n">kwargs</span>
        <span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">reversible</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">reverse_code</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">state_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="c1"># RunPython objects have no state effect. To add some, combine this</span>
        <span class="c1"># with SeparateDatabaseAndState.</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">database_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="c1"># RunPython has access to all models. Ensure that all models are</span>
        <span class="c1"># reloaded in case any are delayed.</span>
        <span class="n">from_state</span><span class="o">.</span><span class="n">clear_delayed_apps_cache</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">router</span><span class="o">.</span><span class="n">allow_migrate</span><span class="p">(</span><span class="n">schema_editor</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">hints</span><span class="p">):</span>
            <span class="c1"># We now execute the Python code in a context that contains a &#39;models&#39;</span>
            <span class="c1"># object, representing the versioned models as an app registry.</span>
            <span class="c1"># We could try to override the global cache, but then people will still</span>
            <span class="c1"># use direct imports, so we go with a documentation approach instead.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">(</span><span class="n">from_state</span><span class="o">.</span><span class="n">apps</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_backwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">reverse_code</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;You cannot reverse this operation&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">router</span><span class="o">.</span><span class="n">allow_migrate</span><span class="p">(</span><span class="n">schema_editor</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">hints</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reverse_code</span><span class="p">(</span><span class="n">from_state</span><span class="o">.</span><span class="n">apps</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">describe</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;Raw Python operation&quot;</span>

<div class="viewcode-block" id="RunPython.noop"><a class="viewcode-back" href="../../../../../ref/migration-operations.html#django.db.migrations.operations.RunPython.noop">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">noop</span><span class="p">(</span><span class="n">apps</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">):</span>
        <span class="k">return</span> <span class="kc">None</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>